package com.wanxi.product.service;

import com.wanxi.product.core_sevice.SkuService;
import com.wanxi.product.core_sevice.SpuService;
import com.wanxi.product.service.api.dto.*;
import com.wanxi.product.service.api.query.enterprise.SpuQuery;
import com.wanxi.product.service.api.result.CommonResult;
import com.wanxi.product.service.api.util.TypeEnum;
import com.wanxi.product.service.api.vo.admin.*;
import com.wanxi.product.service.api.query.admin.TypeQuery;
import com.wanxi.product.service.api.query.admin.SkuQuery;
import com.wanxi.product.service.api.query.admin.TagQuery;
import com.wanxi.product.service.api.feign.ProductAdminService;
import com.wanxi.product.util.BeanCopyUtils;
import com.wanxi.product.util.Check;
import com.wanxi.product.util.VoToDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.List;

@RestController
@Api(tags = "平台接口")
@RequestMapping("/admin")
public class ProductAdminServiceImpl implements ProductAdminService {
    @Resource
    private SpuService spuService;

    @Resource
    private SkuService skuService;

    @Resource
    private Check check;

    @Override
    @ApiOperation("添加分类")
    @PostMapping("/type/add")
    public CommonResult<Long> addType(TypeAddInVo typeAddInVo) {
        /*只能添加一级二级分类*/
        if (check.checkTypeLevel(typeAddInVo.getParentId()).equals(TypeEnum.二级分类)) {
            return CommonResult.failed("不可添加三级分类");
        }
        TypeDto typeAddInDto = VoToDto.typeVTD(typeAddInVo);
        return CommonResult.success(spuService.addType(typeAddInDto));
    }

    @Override
    @ApiOperation("获取分类列表")
    @GetMapping("/type/findAll")
    public CommonResult<List<TypeFindAllOutVo>> findAllType(TypeQuery typeQuery) {
        List<TypeFindAllOutVo> typeVoList = new ArrayList<>();

        List<TypeDto> typeDtoList = spuService.findAllType(typeQuery);

        /*只需要一级二级分类数据*/
        for (TypeDto typeDto : typeDtoList) {
            TypeEnum thisType = check.checkTypeLevel(typeDto.getTypeId());
            if (thisType.equals(TypeEnum.三级分类)) {
                continue;
            }
            TypeFindAllOutVo typeVo = TypeFindAllOutVo.builder()
                    .typeId(typeDto.getTypeId())
                    .typeName(typeDto.getTypeName())
                    .img(typeDto.getImg())
                    .description(typeDto.getDescription())
                    .state(typeDto.getState())
                    .build();

            if (thisType.equals(TypeEnum.二级分类)) {
                TypeDto firstParentDto = spuService.findTypeByTypeId(typeDto.getParentId());
                typeVo.setFirstParentTypeName(firstParentDto.getTypeName());
            }
            typeVoList.add(typeVo);
        }
        return CommonResult.success(typeVoList);
    }

    @Override
    @ApiOperation("根据id查询分类")
    @ApiImplicitParam(name = "typeId", value = "要查找分类的id", dataType = "Long")
    @GetMapping("/type/findById")
    public CommonResult<TypeFindByIdOutVo> findTypeById(Long typeId) {
        if (check.checkTypeLevel(typeId).equals(TypeEnum.三级分类)) {
            return CommonResult.failed("查找的分类为三级分类");
        }

        TypeDto typeDto = spuService.findTypeByTypeId(typeId);

        TypeFindByIdOutVo typeFindByIdOutVo = TypeFindByIdOutVo.builder()
                .typeName(typeDto.getTypeName())
                .img(typeDto.getImg())
                .description(typeDto.getDescription())
                .state(typeDto.getState())
                .build();
        if (typeDto.getParentId()==0){
            typeFindByIdOutVo.setParentTypeName("无父类");
        }else{
            TypeDto parentTypeDto = spuService.findTypeByTypeId(typeDto.getParentId());
            typeFindByIdOutVo.setParentTypeName(parentTypeDto.getTypeName());
        }
        return CommonResult.success(typeFindByIdOutVo);
    }

    @Override
    @ApiOperation("修改分类")
    @PutMapping("/type/update")
    public CommonResult updateType(TypeUpdateInVo typeUpdateInVo) {
        if (check.checkTypeLevel(typeUpdateInVo.getTypeId()).equals(TypeEnum.三级分类)) {
            return CommonResult.failed("查找的分类为三级分类");
        }

        TypeDto typeDto = VoToDto.typeVTD(typeUpdateInVo);
        return CommonResult.success(spuService.updateType(typeDto));
    }

    @Override
    @ApiOperation("删除分类")
    @ApiImplicitParam(name = "id", value = "要删除分类的id", dataType = "Long")
    @PutMapping("/type/delete")
    public CommonResult deleteType(Long typeId) {
        /*一级分类，删除两次子分类*/
        TypeEnum thisType = check.checkTypeLevel(typeId);
        if (thisType.equals(TypeEnum.一级分类)) {
            TypeQuery typeQuery = TypeQuery.builder()
                    .parentId(typeId)
                    .build();
            List<TypeDto> childTypeDtoList = spuService.findAllType(typeQuery);
            if (!childTypeDtoList.isEmpty()) {
                for (TypeDto dto : childTypeDtoList) {
                    TypeQuery typeQuery1 = TypeQuery.builder()
                            .parentId(dto.getTypeId())
                            .build();
                    List<TypeDto> childTypeDtoList1 = spuService.findAllType(typeQuery1);
                    if (!childTypeDtoList1.isEmpty()) {
                        for (TypeDto dto1 : childTypeDtoList1) {
                            spuService.deleteType(dto1.getTypeId());
                        }
                    }
                    spuService.deleteType(dto.getTypeId());
                }
            }
        } else if (thisType.equals(TypeEnum.二级分类)) {
            TypeQuery typeQuery = TypeQuery.builder()
                    .parentId(typeId)
                    .build();
            List<TypeDto> childTypeDtoList = spuService.findAllType(typeQuery);
            if (!childTypeDtoList.isEmpty()) {
                for (TypeDto dto : childTypeDtoList) {
                    spuService.deleteType(dto.getTypeId());
                }
            }
        }
        spuService.deleteType(typeId);
        return CommonResult.success();
    }

    @Override
    @ApiOperation("获取sku列表")
    @GetMapping("/sku/findAll")
    public CommonResult<List<SkuFindAllOutVo>> findAllSku(SkuQuery skuQuery) {
        /*查询所有符合条件的sku，且去重*/
        Set<SkuFindAllOutVo> set = new HashSet<>();

        /*根据id,name,state查找*/
        SkuQueryDto skuQueryDto = SkuQueryDto.builder()
                .skuId(skuQuery.getSkuId())
                .skuName(skuQuery.getSkuName())
                .state(skuQuery.getState())
                .build();
        List<SkuDto> skuListTemp = skuService.findAllSku(skuQueryDto);
        List<SkuFindAllOutVo> skuVoList = BeanCopyUtils.copyList(skuListTemp, SkuFindAllOutVo.class);

        /*查找分类对应的spu*/
        SpuQuery spuQuery = SpuQuery.builder()
                .typeId(skuQuery.getTypeId())
                .build();
        List<SpuDto> spuDtoList = spuService.findAllSpu(spuQuery);
        for (SpuDto spuDto : spuDtoList) {
            SkuQueryDto skuQueryDto1 = SkuQueryDto.builder()
                    .spuId(spuDto.getSpuId())
                    .build();
            List<SkuDto> skuListTemp1 = skuService.findAllSku(skuQueryDto1);
            for (SkuDto skuDto : skuListTemp1) {
                SkuFindAllOutVo skuFindAllOutVo = SkuFindAllOutVo.builder()
                        .skuId(skuDto.getSkuId())
                        .skuName(skuDto.getSkuName())
                        .img(skuDto.getImg())
                        .state(skuDto.getState())
                        .build();
                set.add(skuFindAllOutVo);
            }
        }

        /*符合条件的sku信息*/
        skuVoList.addAll(set);
        return CommonResult.success(skuVoList);
    }

    @Override
    @ApiOperation("修改sku")
    @PutMapping("/sku/update")
    public CommonResult updateSku(SkuUpdateInVo skuUpdateInVo) {
        SkuDto skuDto = SkuDto.builder()
                .skuId(skuUpdateInVo.getSkuId())
                .state(skuUpdateInVo.getState())
                .build();
        skuService.updateSku(skuDto);
        return CommonResult.success(skuService.updateSku(skuDto));
    }

    @Override
    @ApiOperation("删除sku")
    @ApiImplicitParam(name = "skuId", value = "要删除sku的id", dataType = "Long")
    @PutMapping("/sku/delete")
    public CommonResult deleteSkuById(Long skuId) {
        return CommonResult.success(skuService.deleteSku(skuId));
    }

    @Override
    @ApiOperation("添加标签")
    @PostMapping("/tag/add")
    public CommonResult addTag(TagAddInVo tagAddInVo) {
        TagDto tagDto = VoToDto.TagVTD(tagAddInVo);
        return CommonResult.success(spuService.addTag(tagDto));
    }

    @Override
    @ApiOperation("获取标签列表")
    @GetMapping("/tag/findAll")
    public CommonResult<List<TagFindAllOutVo>> findTagAll(TagQuery tagQuery) {
        List<TagFindAllOutVo> tagVoList = new ArrayList<>();
        List<TagDto> tagDtoList = spuService.findAllTag(tagQuery);
        for (TagDto tagDto : tagDtoList) {
            TagFindAllOutVo tagFindAllOutVo = TagFindAllOutVo.builder()
                    .tagId(tagDto.getTagId())
                    .tagName(tagDto.getTagName())
                    .build();
            tagVoList.add(tagFindAllOutVo);
        }
        return CommonResult.success(tagVoList);
    }

    @Override
    @ApiOperation("逻辑删除标签")
    @ApiImplicitParam(name = "tagId", value = "要逻辑删除的商品标签id", dataType = "Long")
    @PutMapping("/tag/delete")
    public CommonResult deleteTagById(Long tagId) {
        spuService.deleteTag(tagId);
        return CommonResult.success();
    }
}
